import { createRouter, createWebHistory } from 'vue-router'

import { useProjectStore } from '@/stores/projectStore'
import useUserInfoStore from '@/stores/userInfo'
import { ElMessage } from 'element-plus'

//导入组件
import LoginVue from '@/views/Login.vue'
import LayoutVue from '@/views/Layout.vue'


import UserInfoVue from '@/views/user/UserInfo.vue'
import UserResetPasswordVue from '@/views/user/UserResetPassword.vue'
import component from 'element-plus/es/components/tree-select/src/tree-select-option.mjs'
import Projects from '../views/Interface_information_management/Projects.vue'
import Objects from '../views/Interface_information_management/Objects.vue'
import Requirements from '../views/Interface_information_management/Requirements.vue'
import Interfaces from '../views/Interface_information_management/Interfaces.vue'
import Elements from '../views/Interface_information_management/Elements.vue'
import InterfaceTypes from '../views/basic_information/InterfaceTypes.vue'
import IoTypes from '../views/basic_information/IoTypes.vue'
import DataSpecifications from '../views/basic_information/DataSpecifications.vue'
import DataTypes from '../views/basic_information/DataTypes.vue'
import UserManage from '../views/user/UserManage.vue'
import InterfaceDocument from '../views/create_document/InterfaceDocument.vue'
import RequirementDocument from '../views/create_document/RequirementDocument.vue'
//定义路由关系
const routes = [
    { path: '/login', component: LoginVue },
    // {
    //     path: '/', component: LayoutVue,redirect:'/article/manage', children: [
    //         { path: '/article/category', component: ArticleCategoryVue },
    //         { path: '/article/manage', component: ArticleManageVue },
    //         { path: '/user/info', component: UserInfoVue },
    //         { path: '/user/avatar', component: UserAvatarVue },
    //         { path: '/user/resetPassword', component: UserResetPasswordVue }
    //     ]
    // },
    {
        path: '/', component: LayoutVue,redirect:'/iim/projects', children: [
            { path: '/iim/projects', component: Projects },
            { path: '/iim/objects', component: Objects },
            { path: '/iim/requirements', component: Requirements },
            { path: '/iim/interfaces', component: Interfaces },
            { path: '/iim/elements', component: Elements },

            { path: '/bi/interface', component: InterfaceTypes, meta:{requiresAdmin: true} },
            { path: '/bi/io', component: IoTypes, meta:{requiresAdmin: true} },
            { path: '/bi/specifications', component: DataSpecifications, meta:{requiresAdmin: true} },
            { path:  '/bi/data', component: DataTypes, meta:{requiresAdmin: true} },

            { path: '/cd/interface', component: InterfaceDocument },
            { path: '/cd/requirement', component: RequirementDocument },


            { path: '/user/userManage',component: UserManage, meta:{requiresAdmin: true}},
            { path: '/user/info', component: UserInfoVue },
            { path: '/user/resetPassword', component: UserResetPasswordVue }
        ]
    }
]

//创建路由器
const router = createRouter({
    history: createWebHistory(),
    routes: routes
})


// 添加路由守卫
router.beforeEach((to, from, next) => {
    const userInfoStore = useUserInfoStore();
    const projectStore = useProjectStore();
    const isAdmin = userInfoStore.info.permission === 'admin';
    const restrictedRoutes = ['/iim/objects', '/iim/requirements', '/iim/interfaces', '/iim/elements'];
  
    if (restrictedRoutes.includes(to.path) && !projectStore.selectedProject.name) {
      ElMessage.error('请先选择一个项目！');
      next('/iim/projects'); // 重定向到项目管理页面
    } else if (to.matched.some(record => record.meta.requiresAdmin)) {
      if (!isAdmin) {
        ElMessage.error('您没有权限访问此页面！');
        next('/'); // 如果不是管理员，跳转到首页或其他页面
      } else {
        next(); // 如果是管理员，允许访问
      }
    } else {
      next(); // 如果不需要管理员权限，允许访问
    }
  });
//导出路由
export default router
